<?php 
    session_start(); 
    include 'db_connection.php';
    if ($_GET['act'] == 'join') {
        $id = $_GET['id'];
        $sql='SELECT true_name,email FROM admins WHERE id = ' . ($_SESSION['user_id'] ? $_SESSION['user_id'] :  0);
        if (empty($_SESSION['user_id'])) {
            die('<script>alert("请先登录");location.href="login.php";</script>');
        }
        $result = mysqli_query($conn, $sql);
        $info = [];
        if ($result) {
            $info = $result->fetch_assoc();
        }
        $sql = 'SELECT project_name FROM donation_projects WHERE id = ' . $id;
        $result = mysqli_query($conn, $sql);
        $pinfo = $result->fetch_assoc();

        $name = $info['true_name'];
        $email = $info['email'];
        $time = date('Y-m-d H:i:s');
        $p_name = $pinfo['project_name'];

        $sql = "INSERT INTO `volunteers` (`name`, `email`, `signup_time`, `project_name`, `status`)
        VALUES ('$name', '$email', '$time', '$p_name', 1)";
        $result = mysqli_query($conn, $sql);

        if ($result) {
            echo '<script>alert("参与成功");location.href="project.php"</script>';
        }
    }
    $sql = 'SELECT * FROM donation_projects ORDER BY id DESC ';
    $result = mysqli_query($conn, $sql);
    $list = [];
    if ($result) {
        while($row = $result->fetch_assoc()) {
            $list[] = $row;
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>捐助项目展示</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<? require 'common/nav.php'; ?>
<main class="project-container">
    <h1>捐助项目展示</h1>
    <div class="p-box">
        <?php foreach($list as $row) { ?>
        <div class="project">
            <h2><?php echo $row['project_name']; ?></h2>
            <p><?php echo $row['description']; ?></p>
            <button><a href="project.php?act=join&id=<?php echo $row['id']; ?>">参与捐助</a></button>
        </div>
        <?php } ?>
        <!-- <div class="project">
            <h2>项目名称</h2>
            <p>项目描述...</p>
            <button>参与捐助</button>
        </div> -->
        <!-- <div class="project">
            <h2>项目名称</h2>
            <p>项目描述...</p>
            <button>参与捐助</button>
        </div> -->
    </div>
</main>
</body>
</html>